package com.yhsoft.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yhsoft.common.service.TokenService;
import com.yhsoft.common.vo.Result;
import com.yhsoft.sys.controller.request.InterfaceConfigInVo;
import com.yhsoft.sys.entity.BaicInterfaceConfig;
import com.yhsoft.sys.service.IBaicInterfaceConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 接口信息配置表 前端控制器
 * </p>
 *
 * @author yyyyyyyh
 * @since 2023-10-10
 */
@RestController
@Api(tags = {"E-第三方服务配置接口"})
@RequestMapping("/sys/baicInterfaceConfig")
public class BaicInterfaceConfigController {

    @Autowired
    IBaicInterfaceConfigService iBaicInterfaceConfigService;

    @ApiOperation("新增/修改接口配置信息")
    @PostMapping("/setInterfaceConfig")
    public Result<?> setInterfaceConfig(@RequestBody InterfaceConfigInVo inVo) {
        // 不能重复新增
        if (inVo.getId() == null) {
            LambdaQueryWrapper<BaicInterfaceConfig> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BaicInterfaceConfig::getAppId, inVo.getAppId());
            List<BaicInterfaceConfig> baicInterfaceConfigs = iBaicInterfaceConfigService.list(wrapper);
            if (baicInterfaceConfigs.size() > 0) {
                return Result.fail("该接口配置信息已经存在，无法新增！");
            }
        }
        iBaicInterfaceConfigService.setInterfaceConfig(inVo);
        return Result.success();
    }

    @ApiOperation("获取接口配置信息")
    @GetMapping("/getInterfaceConfig")
    public Result<InterfaceConfigInVo> getInterfaceConfig() {
        InterfaceConfigInVo outVo = new InterfaceConfigInVo();
        Integer companyid = TokenService.getUserBytoken().getCompanyId();
        LambdaQueryWrapper<BaicInterfaceConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BaicInterfaceConfig::getCompanyId, companyid);
        BaicInterfaceConfig baicInterfaceConfig = iBaicInterfaceConfigService.getBaseMapper().selectOne(wrapper);
        if(baicInterfaceConfig != null){
            outVo.valueOf(baicInterfaceConfig);
        }
        return Result.success(outVo);
    }
}
